#!/usr/bin/env python
#
# Copyright 2011 Justin Bruce Van Horne <justinvh@gmail.com>
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Creates a rendered scene that is navigatable by the user for demoing.

 PreviewWindow: A windowing class for previwing content from the map editor.
"""

import sys
import os
import pyglet
import json

from pyglet.clock import schedule_interval

from engine.fixed_resolution import FixedResolutionViewport
from engine import camera

from pyglet.window import key


WIDTH, HEIGHT = (256, 224)
UP, DOWN, LEFT, RIGHT = (0, 1, 2, 3)


class PreviewWindow(pyglet.window.Window):
    """Creates a rendered scene that the user can navigate through
    and effictively 'preview' the map."""
    map_editor = None

    def __init__(self):
        """Initialize the flat camera."""
        super(PreviewWindow, self).__init__(WIDTH, HEIGHT)
        self.camera = camera.FlatCamera(0, 0, WIDTH, HEIGHT)
        self._want = None

        # Retro look
        target_resolution = 256,224
        target_width, target_height = target_resolution
        self.viewport = FixedResolutionViewport(self, 
                target_width, target_height, filtered=False)

    def set_map_editor(self, editor):
        """Set the map editor"""
        self.map_editor = editor

    def on_draw(self):
        """Handles the logic for drawing"""
        if not self.map_editor:
            return

        if not self._visible:
            return

        self.clear()
        self.camera.project()
        #self.viewport.begin()
        # Draw the batches in the current context
        for case, batch in self.map_editor.batches:
            batch.draw()
        #self.viewport.end()

    def transition(self, dt, direction):
        """Create transitions for the view. This gives the old
        style transition of previous LOZ games."""
        if direction == UP:
            self.camera.y -= 8
            if self._want >= self.camera.y:
                self.camera.y = self._want
                self._want = None
        elif direction == DOWN:
            self.camera.y += 8
            if self._want <= self.camera.y:
                self.camera.y = self._want
                self._want = None
        elif direction == LEFT:
            self.camera.x -= 8
            if self._want >= self.camera.x:
                self.camera.x = self._want
                self._want = None
        elif direction == RIGHT:
            self.camera.x += 8
            if self._want <= self.camera.x:
                self.camera.x = self._want
                self._want = None

        if self._want is None:
            pyglet.clock.unschedule(self.transition)

    def on_key_press(self, pressed, modifiers):
        """Handle the key events, including transitioning."""
        if pressed == key.W and not self._want:
            self._want = self.camera.y - HEIGHT
            schedule_interval(self.transition, 1 / 60.0, direction=UP)
        elif pressed == key.S and not self._want:
            self._want = self.camera.y + HEIGHT
            schedule_interval(self.transition, 1 / 60.0, direction=DOWN)
        elif pressed == key.D and not self._want:
            self._want = self.camera.x - WIDTH
            schedule_interval(self.transition, 1 / 60.0, direction=LEFT)
        elif pressed == key.A and not self._want:
            self._want = self.camera.x + WIDTH
            schedule_interval(self.transition, 1 / 60.0, direction=RIGHT)
